
// 从URL中获取FastMoss店铺ID
function getFastMossStoreId() {
  const url = window.location.href;
  const match = url.match(/\/detail\/(\d+)(?:#|$)/);
  return match ? match[1] : null;
}

function extractFastmossStoreBasicInfo() {
  const result = {};
  
  // 获取店铺名称
  const storeNameXPath = '//*[@id="container"]/div[2]/div[2]/main/div/div/div[2]/div[2]/div[1]/div[1]/div[3]/span';
  const storeNameNode = getNodeByXPath(storeNameXPath);
  if (storeNameNode) {
    result.storeName = extractNumberFromNode(storeNameNode);
  }

  // 获取店铺类别
  const storeCategoryXPath = '//*[@id="container"]/div[2]/div[2]/main/div/div/div[2]/div[2]/div[1]/div[1]/div[4]/div[2]/span/div/span';
  const storeCategoryNode = getNodeByXPath(storeCategoryXPath);
  if (storeCategoryNode) {
    result.storeCategory = extractNumberFromNode(storeCategoryNode);
  }

  // 获取基本信息键值对
  const containerXPath = '//*[@id="container"]/div[2]/div[2]/main/div/div/div[2]/div[2]/div[1]/div[2]/div';
  const keyValuePairs = extractKeyValuePairs(
    containerXPath,
    "./div[1]",
    "./div[2]"
  );

  return { ...result, ...keyValuePairs };
}

function extractFastmossStoreOverall() {
  const result = {};
  
  // 提取主要数据
  const xpath_overall = '//*[@id="container"]/div[2]/div[2]/main/div/div/div[2]/div[2]/div[2]/div/div/div/div[1]/div';
  const mainDivs = getNodesByXPath(xpath_overall);
  
  for (let i = 0; i < mainDivs.snapshotLength; i++) {
    const div = mainDivs.snapshotItem(i);
    
    // 尝试两种可能的key节点路径
    let keyNode = getNodeByXPath('./div[1]/div[2]/div/div/div/span', div);
    if (!keyNode) {
      keyNode = getNodeByXPath('./div[1]/div[2]/div/div', div);
    }
    
    const valueNode = getNodeByXPath('./div[1]/div[1]/div', div);

    if (keyNode && valueNode) {
      result[getAllTextContent(keyNode)] = extractNumberFromNode(valueNode);
    }
  }

  // 提取额外数据
  const additionalXPath = '//*[@id="container"]/div[2]/div[2]/main/div/div/div[2]/div[2]/div[2]/div/div/div/div[2]/div[3]/div';
  const additionalDivs = getNodesByXPath(additionalXPath);

  for (let i = 0; i < additionalDivs.snapshotLength; i++) {
    const div = additionalDivs.snapshotItem(i);
    const keyNode = getNodeByXPath("./div[2]/span", div);
    const valueNode = getNodeByXPath("./div[1]", div);

    if (keyNode && valueNode) {
      result[getAllTextContent(keyNode)] = extractNumberFromNode(valueNode);
    }
  }

  return result;
}

function extractFastmossStoreTrend28() {
  // 数据趋势
  const labelsXPath = '//*[@id="overview"]/div[2]/div/div/div/div/div/div[1]/div/div/label';
  const labels = getNodesByXPath(labelsXPath);

  const result = {};
  for (let i = 0; i < labels.snapshotLength; i++) {
    const label = labels.snapshotItem(i);
    const keyNode = getNodeByXPath("./span[2]/div/div[2]/p", label);
    const valueNode = getNodeByXPath("./span[2]/div/div[1]/p", label);

    if (keyNode && valueNode) {
      result[extractTextFromNode(keyNode)] = extractNumberFromNode(valueNode);
    }
  }
  return result;
}

function extractFastmossStoreDealAnalysis() {
  // 提取交易渠道数据
  const dealChannel = extractKeyValuePairs(
    '//*[@id="sales_analyze"]/div[2]/div/div/div/div/div[1]/div/div/div/div[2]/div/div[2]/div[position()>1]',
    './div[1]/div/div[2]/span',
    './div[2]/span'
  );

  // 提取交易方式数据
  const dealManner = extractKeyValuePairs(
    '//*[@id="sales_analyze"]/div[2]/div/div/div/div/div[2]/div/div/div/div[2]/div/div[2]/div[position()>1]',
    './div[1]/div/div[2]/span',
    './div[2]/span'
  );

  return { dealChannel, dealManner };
}

function extractFastmossStoreGoodsAnalysis() {
  // 提取商品类别数据
  const goodsCategory = extractKeyValuePairs(
    '//*[@id="goods_analyze"]/div[2]/div/div/div[2]/div[1]/div/div[2]/div/div/div/div[2]/div/div[2]/ul/li',
    './span[1]',
    './span[2]'
  );

  return { goodsCategory };
}

function extractFastmossStoreGoodsList() {
  // 提取表头数据
  const headerNodes = getNodesByXPath(
    '//*[@id="goods_analyze"]/div[3]/div/div[2]/div[2]/div/div/div/div/div/div/div/div/table/thead/tr/th'
  );

  const keys = [];
  for (let i = 0; i < headerNodes.snapshotLength; i++) {
    const text = getAllTextContent(headerNodes.snapshotItem(i));
    if (text) {
      keys.push(text);
    }
  }

  // 提取行数据
  const rowNodes = getNodesByXPath(
    '//*[@id="goods_analyze"]/div[3]/div/div[2]/div[2]/div/div/div/div/div/div/div/div/table/tbody/tr[position()>1]'
  );

  const result = [];
  for (let i = 0; i < rowNodes.snapshotLength; i++) {
    const row = rowNodes.snapshotItem(i);
    const cellNodes = getNodesByXPath('./td', row);

    const rowData = {};
    for (let j = 0; j < cellNodes.snapshotLength && j < keys.length; j++) {
      const text = getAllTextContent(cellNodes.snapshotItem(j));
      if (keys[j]) {
        rowData[keys[j]] = toPureNumber(text);
      }
    }
    result.push(rowData);
  }

  return result;
}

function extractFastmossStoreInfluencerList() {
  // 带货达人列表
  const headerXPath = '//*[@id="author_analyze"]/div[3]/div/div/div[2]/div[2]/div/div/div/div/div/div/div/div/table/thead/tr/th';
  const rowXPath = '//*[@id="author_analyze"]/div[3]/div/div/div[2]/div[2]/div/div/div/div/div/div/div/div/table/tbody/tr[position()>1]';
  
  return extractTableData(headerXPath, rowXPath);
}

function extractFastmossStoreSelfAccountOverall() {
  // 自营账号分析
  
  // 提取第一组数据 (position < 3)
  const firstGroupData = extractKeyValuePairs(
    '//*[@id="self_account_analyze"]/div[2]/div/div/div/div[1]/div[position()<3]/div',
    './div[2]',
    './div[1]'
  );

  // 提取第二组数据 (position 3)
  const secondGroupData = extractKeyValuePairs(
    '//*[@id="self_account_analyze"]/div[2]/div/div/div/div[1]/div[3]/div/div',
    './div[2]',
    './div[1]'
  );

  // 合并数据
  return {
    ...firstGroupData,
    ...secondGroupData
  };
}

function extractFastmossStoreLiveStreamOverall() {
  // 直播总体数据
  return extractKeyValuePairs(
    '//*[@id="live_case"]/div[2]/div/div/div[2]/div/div/div/div',
    './div/div[2]/div/div',
    './div/div[1]/div'
  );
}

function extractFastmossStoreLiveStreamList() {
  // 直播列表数据
  const headerXPath = '//*[@id="live_case"]/div[3]/div[2]/div/div/div/div/div/div/div/div/table/thead/tr/th';
  const rowXPath = '//*[@id="live_case"]/div[3]/div[2]/div/div/div/div/div/div/div/div/table/tbody/tr[position()>1]';
  
  return extractTableData(headerXPath, rowXPath);
}

function extractFastmossStoreVideoOverall() {
  // 视频总体数据
  return extractKeyValuePairs(
    '//*[@id="video_material"]/div/div[2]/div/div/div/div/div',
    './div/div[2]/div/div',
    './div/div[1]/div'
  );
}

function extractFastmossStoreVideoList() {
  // 视频列表数据
  const headerXPath = '//*[@id="video_material"]/div/div[3]/div[3]/div/div/div/div/div/div/div/div/table/thead/tr/th';
  const rowXPath = '//*[@id="video_material"]/div/div[3]/div[3]/div/div/div/div/div/div/div/div/table/tbody/tr[position()>1]';
  
  return extractTableData(headerXPath, rowXPath);
}

function extractFastmossStoreDescription() {
  // 提取店铺描述文本
  const textNodes = document.evaluate(
    '//*[@id="myDiv"]/div/span//text()',
    document,
    null,
    XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
    null
  );

  let description = '';
  for (let i = 0; i < textNodes.snapshotLength; i++) {
    description += textNodes.snapshotItem(i).nodeValue.trim() + ' ';
  }

  return description.trim();
}

export {
  getFastMossStoreId,
  extractFastmossStoreBasicInfo,
  extractFastmossStoreOverall,
  extractFastmossStoreTrend28,
  extractFastmossStoreDealAnalysis,
  extractFastmossStoreGoodsAnalysis,
  extractFastmossStoreGoodsList,
  extractFastmossStoreInfluencerList,
  extractFastmossStoreSelfAccountOverall,
  extractFastmossStoreLiveStreamOverall,
  extractFastmossStoreLiveStreamList,
  extractFastmossStoreVideoOverall,
  extractFastmossStoreVideoList,
  extractFastmossStoreDescription
};
